package com.hiray.demos.visualEffect

import android.content.Context
import android.graphics.*
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.WindowManager.LayoutParams
import android.view.WindowManager.LayoutParams.WRAP_CONTENT
import androidx.appcompat.app.AppCompatActivity


object SlideViewHelper {

    fun attach(activity: AppCompatActivity) {
        var display = activity.resources.displayMetrics
        val windowManager = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val decorView = activity.window.decorView.findViewById<ViewGroup>(android.R.id.content)
        val backArcView = BackArcView(activity)
        val lp = LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
        lp.x = 0
        lp.height = 0
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
        lp.format = PixelFormat.RGBA_8888
        decorView.setOnTouchListener { v, event ->
            val x = event.x
            val y = event.y

            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    windowManager.addView(backArcView, lp)
                }

                MotionEvent.ACTION_MOVE -> {
                    backArcView.updateAnchor(x, y)
                    lp.y = y.toInt()
                    lp.x = 0
                    windowManager.updateViewLayout(backArcView, lp)
                }

                MotionEvent.ACTION_UP -> {
                    windowManager.removeViewImmediate(backArcView)
                }

            }
            true
        }
    }


    class BackArcView(context: Context) : View(context) {

        var curveHeight = 300
        var anchorX: Float = 0f
        var anchorY: Float = 0f
        var path = Path()
        var handlerR = 20
        var paint: Paint? = null

        /*
         * *
         * *
         *   *
         *     *
         *       *
         *          *
         *            *
         *               *
         *                 *
         *                *
         *              *
         *            *
         *         *
         *       *
         *     *
         *   *
         *  *
         * *
         */

        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            if (paint == null) {
                paint = Paint()
                paint!!.style = Paint.Style.FILL_AND_STROKE
                paint!!.color = Color.BLACK
            }
            path.reset()
            path.moveTo(0f, anchorY - curveHeight / 2)
            path.lineTo(0f, anchorY + curveHeight / 2)
            path.quadTo(anchorX / 2, anchorY + curveHeight / 4, anchorX - handlerR, anchorY + handlerR)
            path.quadTo(anchorX, anchorY, anchorX - handlerR, anchorY + handlerR)
            path.quadTo(anchorX / 2, anchorY - curveHeight / 4, 0f, anchorY - curveHeight / 2)
            canvas.drawPath(path, paint)
        }

        fun updateAnchor(x: Float, y: Float) {
            //bizer
            this.anchorX = x
            this.anchorY = y
            requestLayout()
            invalidate()
        }

        override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            setMeasuredDimension(this.anchorX.toInt(), curveHeight)
        }
    }
}